package com.zeng.jiudian.servlet.nav;

import com.alibaba.fastjson.JSONObject;
import com.zeng.jiudian.bean.NavModel;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.service.NavService;
import com.zeng.jiudian.service.impl.NavServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/28 - 16:59
 */
@WebServlet("/back/nav/findAll")
public class NavListServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String navName = req.getParameter("navName");
        String navHref = req.getParameter("navHref");
        String isShow = req.getParameter("isShow");
        String recommend = req.getParameter("recommend");
        String order = req.getParameter("order");
        String lift = req.getParameter("lift");
        String pageSize = req.getParameter("pageSize");
        String pageNum = req.getParameter("pageNum");
        String nowNum = req.getParameter("nowNum");

        NavModel navModel = new NavModel();
        navModel.setNavName(navName);
        navModel.setNavHref(navHref);
        String sortString = req.getParameter("sort");
        int sort = (sortString == null || "".equals(sortString) ? -1 : Integer.parseInt(sortString));
        navModel.setIsShow(isShow == null ? 0 : Integer.parseInt(isShow));
        navModel.setRecommend(isShow == null ? 0 : Integer.parseInt(recommend));
        navModel.setOrder(order);
        navModel.setLift(lift);
        navModel.setSort(sort);

        PageModel pageModel = new PageModel();
        pageModel.setPageNum((pageNum == null || "".equals(pageNum) || "0".equals(pageNum)) ? 1 : Integer.parseInt(pageNum));
        pageModel.setPageSize((pageSize == null || "".equals(pageSize) || "0".equals(pageSize)) ? 10 : Integer.parseInt(pageSize));
        pageModel.setNowNum((nowNum == null || "".equals(nowNum) || "0".equals(nowNum)) ? 1 : Integer.parseInt(nowNum));
        navModel.setPageModel(pageModel);

        NavService navService = new NavServiceImpl();
        List<NavModel> list = navService.findAll(navModel);
        int count = navService.getCount(navModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
